import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import '@/FontAwesome/css/fontawesome.css'
import '@/FontAwesome/css/brands.css'
import '@/FontAwesome/css/solid.css'
import router from "@/Router"
import axios, {Axios} from 'axios';
import VueAxios from 'vue-axios'
import App from './App.vue'
const app=createApp(App)
axios.defaults.baseURL="http://springboot.e0ms.com/"
//axios.defaults.withCredentials=true
axios.defaults.baseURL = '/api'
router.beforeEach(async (to, from,next) => {
    from.path
    if (to.meta.isLogin){
        await axios.get("/User/isLogin/loginMsg").then((req)=>{
            const data=req.data;
            if (data.status !== 200) {
                next("/")
                return false
            }
            if (data.roleId===1){
                if (!to.meta.isSystem){
                    next("/SystemAdmin")
                    return false
                }
            }
            if (data.roleId===2){
                if (!to.meta.isDrugs){
                    next("/Drugs")
                    return false
                }
            }
            if (data.roleId===3){
                if (!to.meta.isDoctor){
                    next("/Doctor")
                    return false
                }
            }
        })
    }
    if (to.meta.isNotLogin) {
        await axios.get("/User/isLogin/loginMsg").then((req) => {
            const data = req.data;
            if (data.status===200) {
                const role=data.data.roleId;
                if (role === 1) {
                    next("/SystemAdmin")
                    return false;
                }
                if (role === 2) {
                    next("/Drugs")
                    return false;
                }
                if (role === 3) {
                    next("/Doctor")
                    return false;
                }
            }
        })

    }
    document.title=to.meta.title;
    next()
})


app.use(ElementPlus).use(router).use(VueAxios,axios)
.mount('#app')
